<template>
  <base-breadcrumb :title="$route.meta.title">
    <!-- 质量管理工作月报 -->
    <div id="base-breadcrumb" style="position: relative; overflow: hidden">
      <!-- 搜索 -->
      <searchData :searchData="searchArr" @search="search"></searchData>
      <!-- 表格 -->
      <a-card :bordered="false">
        <!-- <title-name title="质量月报"></title-name> -->
        <!-- 表格 -->
        <list-table ref="table" tableLayout="fixed" size="default" :columns="columns" :request="loadData">
          <!-- status = 0  公司和分公司  有按钮 节点管理、填报 ，项目部 没有任何按钮; -->
          <!-- status = 1  公司和分公司 有按钮 节点管理 如果 fill为true显示填报 如果adjust为true显示调整（填报和调整只显示其一）分公司有上报按钮    公司看到发布按钮  项目部无按钮 -->
          <!-- status = 2  任何登录都有查看那妞  分公司和公司 有节点管理 调整按钮  -->
          <!-- { CO: '公司', BC: '分公司', PD: '项目' }[this.$currentPost().code] -->
          <!-- <more-button slot="actions" slot-scope="text, record">
            <a v-if="record.look" @click="handleManage(0, record)">查看</a>
            <a v-btnPermission="'qm_053_nod_01'" @click="handleManage(1, record)">节点管理</a>
            <a v-if="record.fill" v-btnPermission="'qm_053_edi_02'" @click="handleManage(2, record)">填报</a>
            <a v-if="record.report" v-btnPermission="'qm_053_rep_04'" @click="handleReport(record)">上报</a>
            <a v-if="record.adjust" v-btnPermission="'qm_053_adj_03'" @click="handleManage(4, record)">调整</a>
            <a v-if="record.release" v-btnPermission="'qm_053_rel_05'" @click="handleManage(5, record)">发布</a>
          </more-button> -->
          <more-button slot="actions" slot-scope="text, record" v-if="record.status == 0 && ($currentPost().code === 'CO' || $currentPost().code === 'BC')">
            <a @click="handleManage(1, record)">节点管理</a>
            <a @click="handleManage(2, record)">填报</a>
          </more-button>
          <more-button slot="actions" slot-scope="text, record" v-else-if="record.status == 1 && ($currentPost().code === 'CO' || $currentPost().code === 'BC')">
            <a  @click="handleManage(1, record)">节点管理</a>
            <a v-if="record.fill" @click="handleManage(2, record)">填报</a>
            <a v-if="record.adjust" @click="handleManage(4, record)">调整</a>
            <a v-if="$currentPost().code === 'CO'" @click="handleManage(5, record)">发布</a>
            <a v-if="$currentPost().code === 'BC'" @click="handleReport(record)">上报</a>
          </more-button>
          <more-button slot="actions" slot-scope="text, record" v-else-if="record.status == 2">
            <a @click="handleManage(0, record)">查看</a>
            <a v-if="($currentPost().code === 'CO' || $currentPost().code === 'BC')" @click="handleManage(1, record)">节点管理</a>
            <a v-if="($currentPost().code === 'CO' || $currentPost().code === 'BC')" @click="handleManage(4, record)">调整</a>
          </more-button>
        </list-table>
      </a-card>
    </div>
  </base-breadcrumb>
</template>

<script>
import { getPage,report } from '@/api/quality/qualityMonth' // api
import BasePage from '@/views/basePage'
import moment from 'moment'
import MoreButton from '@/components/MoreButton'
const searchArr = [
  // 搜索框
  {
    name: '月报时间',
    value: 'checkTime',
    type: 'monthRange',
    attr: {
      rangeFormat: ['startTime', 'endTime'],
      disabledDate(current) {
        return current && current > moment().startOf('day')
      }
    }
  },
  {
    name: '状态',
    type: 'select',
    value: 'status',
    data: [
      {
        value: 0,
        label: '未填报'
      },
      {
        value: 1,
        label: '待发布'
      },
      {
        value: 2,
        label: '已发布'
      }
    ]
  }
]
const columns = [
  {
    title: '序号',
    width: 80,
    dataIndex: 'index',
    customRender() {
      return arguments[2] + 1
    }
  },
  {
    title: '组织',
    dataIndex: 'orgName'
  },
  {
    title: '月报时间',
    dataIndex: 'nowMonthTime',
    customRender(e) {
      return moment(e).format('YYYY-MM')
    }
  },
  {
    title: '状态',
    dataIndex: 'status',
    customRender(e) {
      return ['未填报', '待发布', '已发布'][e]
    }
  },
  {
    title: '操作',
    dataIndex: 'actions',
    width: '200px',
    align: 'center',
    scopedSlots: { customRender: 'actions' }
  }
]
export default {
  name: 'a' + Date.now(),
  extends: new BasePage(),
  components: {
    MoreButton
  },
  data() {
    this.columns = columns
    this.searchArr = searchArr
    return {
      queryParam: {}, // 查询参数
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, { query: this.queryParam })
        return getPage(requestParameters).then((res) => {
          return res
        })
      },
      editModel: 0,
      countList: [
        {
          title: '总数：',
          value: 'total',
          needUnit: true
        }
      ], // 汇总列表
      form: {},
      exportLoading: {
        0: false,
        1: false
      } // 导出loading
    }
  },
  methods: {
    // 搜索
    search(value) {
      value && (this.queryParam = value)
      this.$refs.table.refresh(true)
    },
    /**
     * 操作栏
     * @param {Number} type -操作类型：0 查看；1 节点管理；2 填报；3 上报；4 调整；5 发布
     * @param {Object} record -当前行
     */
    handleManage(type, record) {
      this.editModel = type
      let query = {
        editModel: type,
        treeId: record.treeId,
        id: record.id,
        status: record.status,
        isLook: type === 0
      }
      if(type === 4){
        query.isAdjust = true
      }
      // 年月存在，则带上参数
      if (record?.monthTime) {
        query.monthTime = moment(record.monthTime).format('YYYY-MM')
        query.month = moment(record.monthTime).format('M')
        query.year = moment(record.monthTime).format('YYYY')
      }
      this.$router.push({
        name: 'workMonthlyReportEdit',
        query
      })
    },
    /**
     * 上报月报
     * @param {Object} row -当前行
     */
    handleReport(row) {
      this.$confirm({
        title: '提示',
        content: '确认是否上报？',
        onOk: () => {
          return report({ id: row.id,branchComName:this.$currentPost().name }).then((res) => {
            if (res.data.code === 200) {
              this.$message.success('上报成功！')
              // 刷新当前页
              this.$refs.table.getData()
            }
          })
        }
      })
    }
  }
}
</script>
